package com.simas.screen;

import java.util.Hashtable;

import com.simas.common.CommonFunction;
import com.simas.common.CommonMenu;
import com.simas.common.CustomPersistentObject;
import com.simas.common.CustomResources;
import com.simas.common.DeletePolisInfo;
import com.simas.common.OpenLoginScreen;
//import com.simas.hid.CallHIDServlet;
import com.simas.mobil.CallMBUServlet;
import com.simas.hid.CallHIDServlet;
import com.simas.info.OpenInfoScreen;
import com.simas.component.CellButton;
import com.simas.component.TwoImageButton;
//import com.simas.component.RowButton;

import net.rim.device.api.system.Bitmap;
//import net.rim.device.api.system.Display;
//import net.rim.device.api.system.Display;
//import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
//import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Keypad;
//import net.rim.device.api.ui.Manager;
//import net.rim.device.api.ui.component.BitmapField;
//import net.rim.device.api.ui.component.Dialog;
//import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class WelcomeScreen extends MainScreen implements FieldChangeListener {
	
	CommonFunction cf = new CommonFunction();
	CustomResources cr = new CustomResources();
	CellButton btReset;
	TwoImageButton btSimasInfo;
	TwoImageButton btSimasMobil;
	TwoImageButton btSimasSehat;
	
	CustomPersistentObject cpo = new CustomPersistentObject();
	Hashtable htPersistentData = new Hashtable();
	
	int intCekScreen = 0;
	
	// add close menu
	protected void makeMenu( Menu menu, int instance ) {
    	CommonMenu cm = new CommonMenu();
        menu.add(cm._closeItem);
    }
	
	// back button action
	protected boolean keyDown(int keycode, int status)  {
 	   if(Keypad.key(keycode) == Keypad.KEY_ESCAPE) {
 		   CommonMenu cm = new CommonMenu(this);
 		   cm.onClose();
 		   return true;
        }
        return false;
    }
	
	// action listener
	public void fieldChanged(Field field, int context) {
		if (field == btReset) {
			DeletePolisInfo dpi = new DeletePolisInfo();
			dpi.fnDeleteAllPolis();
		} else if (field == btSimasInfo) {
			OpenInfoScreen ois = new OpenInfoScreen();
			ois.fnOpenInfoScreen(this);
		} else if (field == btSimasMobil) {
			//Dialog.alert("Simas Mobil!");
			int intIdxSelected = 2;
			fnOpenSimasMobil(intIdxSelected);
		} else if (field == btSimasSehat) {
			//Dialog.alert("Simas Sehat!");
			int intIdxSelected = 1;
			fnOpenSimasSehat(intIdxSelected);
		}
	}
	
	public void fnOpenSimasMobil(int intIdxSelected) {
		try {
			htPersistentData = cpo.LoadPersistentObject(intIdxSelected);
		}
		catch (NullPointerException npe) {
			// nothing
		}
		int intPersistentData = htPersistentData.size();
		if (intPersistentData > 0) {
			Hashtable htData = new Hashtable();
			htData = (Hashtable) htPersistentData.get("myData");
			
			if (htData.size() > 0) {
				String strNoPlat = htData.get("NOPLAT").toString();
				String strNoPolis = htData.get("NOPOLIS").toString();
				String strNoMesin = htData.get("NOMESIN").toString();;
				String strNoRangka = htData.get("NORANGKA").toString();

				CallMBUServlet cms = new CallMBUServlet();
				cms.fnCallMBUServlet(this, strNoPlat, strNoPolis, strNoRangka, strNoMesin);
			} else {
				//fnOpenLoginScreen(intIdxSlct);
				OpenLoginScreen ols = new OpenLoginScreen();
				ols.fnOpenLoginScreen(this, intIdxSelected);
			}
		} else {
			//fnOpenLoginScreen(intIdxSlct);
			OpenLoginScreen ols = new OpenLoginScreen();
			ols.fnOpenLoginScreen(this, intIdxSelected);
		}
	}
	
	public void fnOpenSimasSehat(int intIdxSelected) {
		try {
			htPersistentData = cpo.LoadPersistentObject(intIdxSelected);
		} 
		catch (NullPointerException npe) {
			// nothing
		}
		
		int intPersistentData = htPersistentData.size();
		if (intPersistentData > 0) {
			Hashtable htData = new Hashtable();
			htData = (Hashtable) htPersistentData.get("myData");
			
			if (htData.size() > 0) {
				String strNama = htData.get("NAMA").toString();
				String strNoKartu = htData.get("NOKARTU").toString();
				String strNoPolis = htData.get("NOPOLIS").toString();
				String strNoPeserta = htData.get("NOPESERTA").toString();
				String strTglLahir = htData.get("TGLLAHIR").toString();

				CallHIDServlet chs = new CallHIDServlet();
				chs.fnCallHIDServlet(this, strNoKartu, strNoPolis, strNoPeserta, strTglLahir, strNama);
			} else {
				OpenLoginScreen ols = new OpenLoginScreen();
				ols.fnOpenLoginScreen(this, intIdxSelected);
			}
		} else {
			OpenLoginScreen ols = new OpenLoginScreen();
			ols.fnOpenLoginScreen(this, intIdxSelected);
		}
	}
	
	public WelcomeScreen() {
		
		Bitmap resizeBitmap = cf.getScaledBitmapImage(cr.getImageURL("background1.png"));
		Background bg = BackgroundFactory.createBitmapBackground(resizeBitmap, 
																 Background.POSITION_X_LEFT, 
																 Background.POSITION_Y_TOP, 
																 Background.REPEAT_NONE);

		VerticalFieldManager vfm = new VerticalFieldManager(Field.USE_ALL_WIDTH);
		vfm.setBackground(bg);
		vfm.setFont(cr.getCustomFont(8));
		vfm.add(new LabelField(" ", Field.USE_ALL_WIDTH));
		
		
		// btReset
		btReset = new CellButton("", Integer.parseInt("FFFF00", 16), 
				 Integer.parseInt("FFCC33", 16), 
				 0, null, 0, 30, 85, 70);
		btReset.setChangeListener(this);
		
		HorizontalFieldManager hfmButton = new HorizontalFieldManager(Field.FIELD_RIGHT);
		hfmButton.add(btReset);
		vfm.add(hfmButton);
		
		VerticalFieldManager blank1 = new VerticalFieldManager(Field.USE_ALL_WIDTH);
		for (int a=1; a<=2; a++) {
			blank1.add(new LabelField(" ", Field.USE_ALL_WIDTH));
		}
		vfm.add(blank1);
		
		btSimasInfo = new TwoImageButton("", Bitmap.getBitmapResource(cr.getImageURL("SimasInfo0.png")), Bitmap.getBitmapResource(cr.getImageURL("SimasInfo1.png")), 0, 0);
		btSimasInfo.setChangeListener(this);
		
		btSimasMobil = new TwoImageButton("", Bitmap.getBitmapResource(cr.getImageURL("SimasMobil0.png")), Bitmap.getBitmapResource(cr.getImageURL("SimasMobil1.png")), 0, 0);
		btSimasMobil.setChangeListener(this);
		
		btSimasSehat = new TwoImageButton("", Bitmap.getBitmapResource(cr.getImageURL("SimasSehat0.png")), Bitmap.getBitmapResource(cr.getImageURL("SimasSehat1.png")), 0, 0);
		btSimasSehat.setChangeListener(this);
		
		HorizontalFieldManager hfmButton1 = new HorizontalFieldManager(Field.FIELD_HCENTER);
		hfmButton1.add(btSimasInfo);
		hfmButton1.add(new LabelField(" ", Field.FIELD_LEFT));
		hfmButton1.add(new LabelField(" ", Field.FIELD_LEFT));
		hfmButton1.add(new LabelField(" ", Field.FIELD_LEFT));
		hfmButton1.add(btSimasMobil);
		hfmButton1.add(new LabelField(" ", Field.FIELD_LEFT));
		hfmButton1.add(new LabelField(" ", Field.FIELD_LEFT));
		hfmButton1.add(new LabelField(" ", Field.FIELD_LEFT));
		hfmButton1.add(btSimasSehat);
		vfm.add(hfmButton1);
		
		VerticalFieldManager blank2 = new VerticalFieldManager(Field.USE_ALL_WIDTH);
		for (int a=1; a<=3; a++) {
			blank2.add(new LabelField(" ", Field.USE_ALL_WIDTH));
		}
		vfm.add(blank2);
		
		add(vfm);
	}
}
